﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using WeifenLuo.WinFormsUI.Docking;
using QLab.UX.Forms;
using QLab.UX.Forms.Controls;
using QLab.UX.Engine;

namespace QLab.UX
{
    public partial class MainForm : Form
    {
        private bool saveLayoutOnClose = true;
        private SolutionExplorerWindow solutionExplorerWindow;
        private ConsoleWindow consoleWindow;
        private VariablesWindow variablesWindow;

        public MainForm()
        {
            InitializeComponent();
            InitializeComponentQLab();
        }

        private void InitializeComponentQLab()
        {
            this.solutionExplorerWindow = new SolutionExplorerWindow(mnuSolutionExplorer, mainDockPanel);
            this.consoleWindow = new ConsoleWindow(mnuConsole, mainDockPanel);
            this.variablesWindow = new VariablesWindow(mnuVariables, mainDockPanel);
            
            this.solutionExplorerWindow.FunctionOpened += new SolutionExplorerWindow.FunctionOpenedDelegate(solutionExplorerWindow_FunctionOpen);
            this.consoleWindow.CommandExecuted += new ConsoleWindow.CommandExecutedEventHandler(consoleWindow_CommandExecuted);
        }

        private void MainForm_Load(object sender, System.EventArgs e)
        {
            string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");

            if (File.Exists(configFile))
            {
                this.mainDockPanel.LoadFromXml(configFile, new DeserializeDockContent(GetContentFromPersistString));
            }

            SetViewCheckBoxes();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
            if (this.saveLayoutOnClose)
                //SB: razmisliti o cuvanju config fajla na user specific lokaciji
                this.mainDockPanel.SaveAsXml(configFile);
            else if (File.Exists(configFile))
                File.Delete(configFile);
        }

        #region Tool WIndows
        #region Console Window
        void consoleWindow_CommandExecuted(object sender, CommandExecutedEventArgs e)
        {
            if (e.Command.Trim() != string.Empty)
                MessageBox.Show("Command: " + e.Command);
        }
        #endregion

        #region Solution Explorer Window
        void solutionExplorerWindow_FunctionOpen(object sender, FunctionOpenedEventArgs e)
        {
            string filePath = e.Function.Path;
            if(!SolutionManager.Instance.ActiveSolution.OpenedFunctions.ContainsKey(filePath))
            {
                FunctionEditor fe = new FunctionEditor(e.Function);
                SolutionManager.Instance.ActiveSolution.OpenedFunctions.Add(filePath, e.Function);
                fe.Show(mainDockPanel);
                
                fe.FormClosing += new FormClosingEventHandler(fe_FormClosing);
            }
        }
        #endregion

        private IDockContent GetContentFromPersistString(string persistString)
        {
            if (EnumsEngine.GetToolWindowType(persistString) == ToolWindowType.SolutionExplorer)
                return solutionExplorerWindow;
            else if (EnumsEngine.GetToolWindowType(persistString) == ToolWindowType.Console)
                return consoleWindow;
            else if (EnumsEngine.GetToolWindowType(persistString) == ToolWindowType.Variables)
                return variablesWindow;
            else
                return null;
        }
        #endregion

        #region Function Editors
        void fe_FormClosing(object sender, FormClosingEventArgs e)
        {
            FunctionEditor fe = sender as FunctionEditor;
            if (fe.Function.Changed)
            {
                if (MessageBox.Show(MainForm_Resources.SaveChangesQuestion, "Warning", MessageBoxButtons.YesNo)
                    == DialogResult.Yes)
                    fe.Function.Save();
            }
            SolutionManager.Instance.ActiveSolution.OpenedFunctions.Remove(fe.Function.Path);
        }
        #endregion

        #region Menus
        #region Click Events
        private void newProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewProjectForm newProjectForm = new NewProjectForm();
            if (newProjectForm.ShowDialog() == DialogResult.OK)
            {
                SolutionManager.Instance.CreateSolution(newProjectForm.SolutionName,
                    newProjectForm.ProjectName, newProjectForm.LocationPath);
                this.solutionExplorerWindow.Reload();
            }
        }

        private void openProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(SolutionManager.Instance.ActiveSolution != null)
            {
                if (MessageBox.Show(MainForm_Resources.CloseActiveAndOpenNewSolutionQuestion, "Warning", MessageBoxButtons.YesNo)
                    == DialogResult.Yes)
                    SolutionManager.Instance.CloseSolution();
                else return;
            }

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.AddExtension = false;
            ofd.CheckFileExists = true;
            ofd.CheckPathExists = true;
            ofd.Multiselect = false;
            ofd.Filter = "QLabSolution(*.qlsln)|*.qlsln";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string path = ofd.FileName;
                SolutionManager.Instance.LoadSolution(path);
                this.solutionExplorerWindow.Reload();
            }
        }

        private void closeSolutionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SolutionManager.Instance.CloseSolution();
            this.solutionExplorerWindow.Reload();
        }

        private void saveAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SolutionManager.Instance.ActiveSolution.SaveAll();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        #endregion

        #region Visibility
        private void fileToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
        {
            if (SolutionManager.Instance.ActiveSolution == null)
            {
                mnuCloseSolution.Enabled = false;
                mnuSaveAll.Enabled = false;
            }
            else
            {
                mnuCloseSolution.Enabled = true;
                mnuSaveAll.Enabled = true;
            }
        }

        private void SetViewCheckBoxes()
        {
            if (!solutionExplorerWindow.Visible) mnuSolutionExplorer.Checked = false;
            if (!consoleWindow.Visible) mnuConsole.Checked = false;
            if (!variablesWindow.Visible) mnuVariables.Checked = false;
        }
        #endregion
        #endregion
    }
}